package com.auto.learning.store.core;

import android.app.Application;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.BaseDaoEnabled;

/**
 * [一句话功能简述]<BR>
 * [功能详细描述]
 * 
 * @author 冯伟立
 * @version [CubeAndroid, 2013-6-9]
 */
public class ModelCreator {

	private ModelCreator() {

	}

	private MutilDBSupport mutilDBSupport = null;

	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述] <BR>
	 * ModelCreator 2013-6-9 下午4:01:33
	 */
	public static ModelCreator build(Application aContext) {
		return build(aContext, null);
	}

	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param aContext
	 * @param dbName
	 * @return 2013-6-9 下午4:04:03
	 */
	public static ModelCreator build(Application aContext, String dbName) {
		return build(aContext, dbName, -1);
	}

	public static ModelCreator build(Application aContext, String dbName,
			int dbVersion) {
		ModelCreator modelCreator = new ModelCreator();
		if (dbVersion != -1) {
			modelCreator.mutilDBSupport = MutilDBSupport.build(aContext,
					dbName, dbVersion);
		} else if (dbName != null) {
			modelCreator.mutilDBSupport = MutilDBSupport
					.build(aContext, dbName);
		} else {
			modelCreator.mutilDBSupport = MutilDBSupport.build(aContext);
		}
		return modelCreator;
	}

	public <ID,T extends BaseDaoEnabled<T, ID>> T createModel(Class<T> clazz) {
		T baseDaoEnabled = null;
		try {
			baseDaoEnabled = clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
			return null;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
		Dao<T, ID> dao = mutilDBSupport.getDao(clazz);

		baseDaoEnabled.setDao(dao);
		return baseDaoEnabled;
	}

}
